/*
 * StartupScreen.java
 *
 * Created on March 2, 2009, 7:43 PM
 */

package gui;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import kernel.XMLPluginConfiguration;
import org.jdom.Document;
import org.jdom.Element;

/**
 *
 * @author  alvaro
 */
public class WelcomeScreen extends javax.swing.JFrame {
    private String s = System.getProperty("file.separator");
    /** Creates new form StartupScreen */
    public WelcomeScreen() {
        this.setUndecorated(true);
        initComponents();
        loadLanguage();
    }

    public void loadLanguage(){
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+File.separator+
                "files"+File.separator+"language.xml");
        String lang = xml.getDoc().getRootElement().getAttributeValue("selected");
        loadLanguage(lang);
    }

    private void loadLanguage(String lang){
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+File.separator+
                "files"+File.separator+"language.xml");

        jCheckBox1.setText(xml.getTag("/languages/"+lang+"/windows/welcome/show_on_startup_label"));
        jButton1.setText(xml.getTag("/languages/"+lang+"/windows/welcome/close_button"));
        try {
            this.jEditorPane1.setPage(new File(Factory.getWorkspace()+s+"files"+s+
                    xml.getTag("/languages/"+lang+"/windows/welcome/welcome_file")).toURI().toURL());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void dispose(){
        Factory.makeGUISingleton().setEnabled(true);
        super.dispose();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jEditorPane1.setEditable(false);
        jScrollPane1.setViewportView(jEditorPane1);

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Mostrar al inicio");
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Cerrar ventana");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 334, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
// TODO add your handling code here:
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+s+"files"+s+"appearance.xml");
        Document doc = xml.getDoc();
        Element root = doc.getRootElement();
        List l = root.getChildren();
        Element start = (Element) l.get(3);
        if(this.jCheckBox1.isSelected()){
            start.setText("yes");
            Factory.makeGUISingleton().setStartup(true);
        }else{
            start.setText("no");
            Factory.makeGUISingleton().setStartup(false);
        }
        
        doc.setRootElement(root);
        xml.dumpFile(Factory.getWorkspace()+s+"files"+s+"appearance.xml");
        
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WelcomeScreen().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
